(async () => {
    // 请确保这些函数在当前页面上下文中可用 (已由页面脚本定义)
    // setByte(address, value); // 修改后可以传入 updateUI=false for silent updates if needed, though default is true
    // getByte(address); // 修改后可以传入 updateUI=false for silent reads if needed, though default is true
    // selectAddressInGrid(address); // 客户端更新UI的方法

    const MY_PRIVATE_ADDR_START = 0;
    const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms)); // 用于模拟延时
    
    // 菲茨杰拉德的名言
    const quote = "The test of a first-rate intelligence is the ability to hold two opposed ideas in the mind at the same time, and still retain the ability to function";
    console.log("[Program] 准备输出名言: " + quote);
    
    console.log("[Program] 开始将名言字符写入内存地址，从 " + MY_PRIVATE_ADDR_START + " 开始...");
    
    // 将名言的每个字符写入连续的内存地址
    for (let i = 0; i < quote.length; i++) {
        const address = MY_PRIVATE_ADDR_START + i;
        const charCode = quote.charCodeAt(i); // 获取字符的ASCII码
        setByte(address, charCode);
        selectAddressInGrid(address); // 更新UI
        console.log(`[Program] 地址 ${address} 写入: '${quote[i]}' (ASCII: ${charCode})`);
        await sleep(50); // 短暂延迟，便于观察
    }
    
    console.log("[Program] 写入完成。开始从内存读取并验证...");
    await sleep(200);
    
    // 从内存读取字符并拼接成字符串
    let readQuote = "";
    for (let i = 0; i < quote.length; i++) {
        const address = MY_PRIVATE_ADDR_START + i;
        const charCode = getByte(address);
        selectAddressInGrid(address); // 更新UI
        readQuote += String.fromCharCode(charCode);
        await sleep(30); // 短暂延迟，便于观察
    }
    
    console.log("[Program] 读取完成。结果:");
    console.log("[Program] " + readQuote);
    
    // 验证读取结果是否正确
    if (readQuote === quote) {
        console.log("[Program] 验证成功: 读取内容与原始名言一致");
    } else {
        console.log("[Program] 验证失败: 读取内容与原始名言不一致");
    }

    console.log("--- 程序运行完毕 ---");
})();
